const mongoose = require('mongoose');
const User = require('../models/user.model');
var   jwt  = require('jsonwebtoken');

//新增
exports.create = function(req, res, next) {
  const user = new User(req.body);
  user.save().then(data => {
    res.json(data);
  });
};

exports.update = function(req, res, next) {
  const id = req.params.id;
  User.findByIdAndUpdate(id, { $set: req.body }, { new: false }).then(data => {
    res.json(data); //这里返回的是修改前的数据，不是修改后的。
  });
};

exports.remove = function(req, res, next) {
  const id = req.params.id;
  User.findByIdAndDelete(id, (err, data) => {
    res.json(data);
  });
};

exports.list = function(req, res, next) {
  //   jwt.verity(req.token, 'secret', (err, authData) => {
  // if (err) {
  // //   res.sendStatus(403);
  //     console.log(err);
  // } else {
  var page = req.body.page ? req.body.page : 1;
  var rows = req.body.rows ? req.body.rows : 1;

  if (req.body.userName && req.body.userName.trim().length > 0) {
        userName       = req.body.userName;
    var str            = '(.*)' + userName + '(.*)';
        queryCondition = {
      userName: new RegExp(str, 'g')
    };
  } else {
    //显示全部数据
    userName       = req.body.userName;
    queryCondition = {
      userName: new RegExp(userName, 'i')
    };
  }
  User.paginate(queryCondition, { page: +page, limit: +5 }, function(
    err,
    result
  ) {
    result.rows = result.docs;
    delete result.docs;
    res.json(result);
  });
  // }
  //   });
};

exports.getdata = function(req, res, next) {
  var id = req.params.id;
  User.findById(id).then(data => {
    res.json(data);
  });
};

exports.removes = function(req, res, next) {
  var ids = req.body.ids.split(',');
  if (ids.length > 0) {
    User.remove({ _id: { $in: ids } }).then(data => {
      res.json({ message: 'delete ok', status: 200 });
    });
  }
};

exports.cateusers_0 = function(req, res, next) {
  //普通用户
  var userType = req.params.userType;
  User.find({ userType: '5be15d95268e7c792cf77020' }).then(data => {
    res.json(data);
  });
};

exports.cateusers_1 = function(req, res, next) {
  //商家用户
  var userType = req.params.userType;
  User.find({ userType: '5be15db0268e7c792cf77021' }).then(data => {
    res.json(data);
  });
};

exports.login = function(req, res, next) {
  // const user = {
  //     id  : 1,
  //     name: rtyu,
  //     age : 67
  // }
  // jwt.sign({ user }, 'secret', (err, token) => {
  //     res.json({ token });
  // })

  var queryCondition = null;
  if (req.body.loginName && req.body.loginPwd) {
    var loginName      = req.params.loginName;
    var loginPwd       = req.params.loginPwd;
        queryCondition = {
      loginName: req.body.loginName,
      loginPwd : req.body.loginPwd
    };
    User.find({
      loginName: req.body.loginName,
      loginPwd : req.body.loginPwd
    }).then(data => {
      if (data.length > 0) {
        jwt.sign({ queryCondition }, 'secret', (err, token) => {
          res.json({ token });
        });
      } else {
        res.json({ status: 404 });
      }
    });

    // function verifyToken(req,res,next){
    //     const bearer
    // }
  } else {
    res.json('不填就算了');
  }

  // jwt.sign({loginName,loginPwd},'secret',(err,token)=>{
  //     res.json({token});
  // })
};
